package com.hucheng.common.utils;

import com.hucheng.common.constant.Constants;
import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Map;
import java.util.UUID;

/**
 * @Author: 凉白开不加冰
 * @Version: 0.0.1V
 * @Date: 2019-06-26
 * @Description: 类描述
 **/
@Slf4j
public class StringUtils {

    private static final String CHARSETNAME_DEFAULT = "UTF-8";

    public static StringUtils getInstance(){
        return new StringUtils();
    }

    /**
     * 获取去掉横线的长度为32的UUID串.
     *
     * @author WuShuicheng.
     * @return uuid.
     */
    public static String get32UUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }


    public String nullToStr(String str) {

        if (str == null) {
            str = "";
        }

        return str;
    }

    public String nullToStrTrim(String str) {

        if (str == null) {
            str = "";
        }

        return str.trim();
    }

    public String nullToUnknown(String str) {

        if (ValidatorUtils.isEmpty(str)) {
            str = Constants.UNKNOWN;
        }

        return str.trim();
    }

    public int nullToIntZero(String str) {

        if (str == null || str.trim().length() == 0) {
            str = "0";
        }

        return Integer.valueOf(str.trim(), 10);
    }

    public String byte2hex(byte[] b) {

        String str = "";
        String stmp = "";

        int length = b.length;

        for (int n = 0; n < length; n++) {
            stmp = (Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1) {
                str += "0";
            }
            str += stmp;
        }

        return str.toLowerCase();
    }

    public String encode(String str, String enc) {

        String strEncode = "";

        try {
            if (str != null)
                strEncode = URLEncoder.encode(str, enc);
        } catch (UnsupportedEncodingException e) {
            log.error(CommonUtils.getTraceInfo() +" str:"+str+"  enc:"+enc+"\t"+ nullToStrTrim(e.getMessage()));
        }

        return strEncode;
    }

    public String decode(String str, String enc) {

        String strDecode = "";

        try {
            if (str != null)
                strDecode = URLDecoder.decode(str, enc);
        } catch (UnsupportedEncodingException e) {
            log.error(CommonUtils.getTraceInfo() +" str:"+str+"  enc:"+enc+"\t"+ nullToStrTrim(e.getMessage()));
        }

        return strDecode;
    }

    public String encode(String str) {

        return encode(str, CHARSETNAME_DEFAULT);
    }

    public String decode(String str) {

        return decode(str, CHARSETNAME_DEFAULT);
    }

    public boolean isHalfAngle(String str) {

        str = nullToStrTrim(str);
        return str.length() == getWordLength(str);
    }

    public int getWordLength(String str) {

        str = nullToStr(str);
        return str.replaceAll("[^\\x00-\\xff]", "**").length();
    }

    public boolean isBlank(String str) {

        int length = 0;

        if ((str == null) || ((length = str.length()) == 0)) {
            return true;
        }

        for (int i = 0; i < length; i++) {
            if (Character.isWhitespace(str.charAt(i))) {
                return true;
            }
        }

        return false;
    }

    public int getRealLength(String str) {

        return getRealLength(str, Constants.CHARSETNAME_DEFAULT);
    }

    public int getRealLength(String str, String charsetName) {

        str = nullToStrTrim(str);

        if (ValidatorUtils.isEmpty(str)) {
            return 0;
        }

        try {
            return str.getBytes(charsetName).length;
        } catch (UnsupportedEncodingException e) {
            log.error(CommonUtils.getTraceInfo() +" str:"+str+"  charsetName:"+charsetName+"\t"+ nullToStrTrim(e.getMessage()));
            return 0;
        }
    }

    public  boolean isNullOrEmpty(String string) {
        // string.isEmpty() in Java 6
        return string == null || string.length() == 0;
    }

    /**
     * 截取指定字符串的指定长度
     * @param str 指定字符串
     * @param length 截取长度
     * @return 返回截取后的字符串
     */
    public  String stringInterception(String str,int length){
        if(isNullOrEmpty(str)){
            return str;
        }
        if(str.length()<length&&str.replaceAll("[^\\x00-\\xff]", "***").length()<length){
            return str;
        }
        //当用户不为空的时候
        //一个汉字 对应 数据库3个字节,防止出错,多截取一个汉字的位置
        char [] c = str.toCharArray();
        int i_length = 0;
        for (int i = 0; i < c.length; i++) {
            if((c[i]+"").matches("[^\\x00-\\xff]")){
                i_length+=3;
            }else{
                i_length+=1;
            }
            if(i_length>length){
                i_length = i-2;
                break;
            }
        }
        return str.substring(0,i_length);
    }

    /**
     * 手机号隐藏中间四位
     * @param phone 手机号
     * @return
     */
    public String hidePhone(String phone){
        if(isBlank(phone)){
            return null;
        }
        return phone.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");
    }
}
